package com.atu.atugra.tools;

import com.atu.atugra.constant.AppHttpCodeEnum;
import lombok.Data;

@Data
public class CommonResult<T> {
    private Integer code;
    private String message;
    private T data;


    //成功通用返回
    public static CommonResult okResult(Object data){
        CommonResult commonResult = new CommonResult(AppHttpCodeEnum.SUCCESS.getCode(),AppHttpCodeEnum.SUCCESS.getMsg());
        if(data!=null){
            commonResult.setData(data);
        }return commonResult;
    }
    //失败通用返回 需要传失败码
    public static CommonResult failed(AppHttpCodeEnum appHttpCodeEnum){
        CommonResult commonResult = new CommonResult(appHttpCodeEnum.getCode(),appHttpCodeEnum.getMsg());
        return commonResult;
    }
    public static CommonResult failed(Integer code, String message){
        CommonResult commonResult = new CommonResult(code,message);
        return commonResult;
    }

    public CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public CommonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }


    @Override
    public String toString() {
        return "CommonResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
